function AlwaysFireBeforeFormSubmit(eventTarget, eventArgument) {
    alert("ho");
    var oldDoPostBack = window["__oldDoPostBack"];
    if(oldDoPostBack == undefined) {
        return;
    }
    var oldDoPostBackFunc = stringToFunction(oldDoPostBack);
    return oldDoPostBackFunc(eventTarget, eventArgument);
}
function mypresubmit() {
    alert("Hej");
    return true;
}
$(document).ready(function () {
    $(".txtJSON").hide(0, null);
    $(".dunite-error").hide(0, null);
    $("button").button();
    $(".dunite-viewButtons").buttonset();
    handlepage.prototype.processJSON($(".txtJSON").val());
});
var stringToFunction = function (str) {
    var arr = str.split(".");
    var fn = (window || this);
    for(var i = 0, len = arr.length; i < len; i++) {
        fn = fn[arr[i]];
    }
    if(typeof fn !== "function") {
        throw new Error("function not found");
    }
    return fn;
};
var handlepage = (function () {
    function handlepage() { }
    handlepage.prototype.processJSON = function (theIpObject) {
        if(theIpObject == undefined) {
            return;
        }
        if(theIpObject == "") {
            return;
        }
        var jsonObject = $.parseJSON(theIpObject);
        if(!jsonObject) {
            alert("JSON decode error");
            return;
        }
        formObject = ko.mapping.fromJS(jsonObject);
        var mystring = window["DuniteViewModel"];
        if(mystring == undefined) {
            return;
        }
        var ViewModelClass = stringToFunction(mystring);
        var viewModel = new ViewModelClass();
        viewModel.Apply(formObject);
        ko.applyBindings(formObject);
    };
    handlepage.prototype.ChangeView = function (name) {
        $(".infoRoadAction").val("ChangeView");
        $(".infoRoadParameters").val(name);
        var jsonObject = ko.mapping.toJSON(formObject);
        $(".txtJSON").val(jsonObject);
        var currentViewName = formObject.Wedding["InfoRoad.Context"].CurrentView["@Name"].peek();
        var acceptEnum = formObject.Wedding["InfoRoad.Context"].CurrentView["@Accept"].peek();
        if(acceptEnum === "NonValid") {
            var myForm;
            myForm = document.forms[0];
            myForm.submit();
        } else {
            if(DuniteValidator.checkForm()) {
                var myForm;
                myForm = document.forms[0];
                myForm.submit();
            } else {
                ($(".dunite-viewButtons input[value='view" + currentViewName + "'] ")[0]).checked = true;
                ($(".dunite-viewButtons input[value='view" + name + "'] ")[0]).checked = false;
                $(".dunite-viewButtons").buttonset("refresh");
                DuniteValidator.showErrors();
                $(".dunite-error").show(500, null);
            }
        }
        return;
        var to = "Save.aspx/SaveForm";
        var dataJSON = ko.toJSON(formObject);
        var options = {
            type: "POST",
            url: to,
            data: dataJSON,
            contentType: "application/json",
            dataType: "json",
            async: false,
            success: this.redirect,
            error: this.showFailure
        };
        $.ajax(options);
    };
    handlepage.prototype.ListForms = function (e) {
        $("#infoRoadAction").val("Redirect");
        $("#infoRoadParameters").val("ListForms.aspx");
        window.location.href = "ListForms.aspx";
    };
    handlepage.prototype.sendJSON = function (form) {
        if($("#infoRoadAction").val() === "Redirect") {
            return;
        }
        if($("#infoRoadAction").val() === "ChangeView") {
            return;
        }
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        var to = "InfoTrail.svc/GetData";
        var dataJSON = ko.toJSON(formObject);
        var options = {
            type: "POST",
            url: to,
            data: '{"value":' + JSON.stringify(dataJSON) + ',"filename":"' + vars['FileName'] + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: this.showResult,
            error: this.showFailure
        };
        $.ajax(options);
    };
    handlepage.prototype.getUrlVars = function () {
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++) {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    };
    handlepage.prototype.SaveForm = function () {
        $(".infoRoadAction").val("save");
        var jsonObject = ko.mapping.toJSON(formObject);
        $(".txtJSON").val(jsonObject);
        var myForm;
        myForm = document.forms[0];
        myForm.submit();
    };
    handlepage.prototype.SuperMitForm = function () {
        $(".infoRoadAction").val("submit");
        var jsonObject = ko.mapping.toJSON(formObject);
        $(".txtJSON").val(jsonObject);
        var myForm;
        myForm = document.forms[0];
        myForm.submit();
    };
    handlepage.prototype.CloseForm = function () {
        $(".infoRoadAction").val("cancel");
        var jsonObject = ko.mapping.toJSON(formObject);
        $(".txtJSON").val(jsonObject);
        var myForm;
        myForm = document.forms[0];
        myForm.submit();
    };
    handlepage.prototype.SubmitForm = function (form) {
        handlepage.prototype.sendJSON(form);
        return true;
    };
    handlepage.prototype.validateSubmit = function (form) {
        $("div.error").hide();
        form.submit();
    };
    handlepage.prototype.showResult = function (response) {
        alert("success: " + response);
    };
    handlepage.prototype.showFailure = function (jqXHR, textStatus, errorThrown) {
        alert("fail: " + textStatus);
    };
    handlepage.prototype.redirect = function (url) {
        var name = $("#infoRoadParameters").val(name);
        window.location.href = name;
    };
    return handlepage;
})();
